<template>
  <div class="xtx-dialog" v-show="visible" :class="{fade}">
    <div class="wrapper" :class="{fade}">
      <div class="header">
        <h3>{{title}}</h3>
        <a @click="closeDialog()" href="javascript:;" class="iconfont icon-close-new"></a>
      </div>
      <div class="body">
        <slot name="body"/>
      </div>
      <div class="footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { watch } from '@vue/runtime-core'
export default {
  name: 'XtxDialog',
  components: { },
  props: {
    title: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    const fade = ref(false)
    // visiable的值为true时展开对话框 否则关闭对话框（控制fade的值）
    watch(() => props.visible, (newVal) => {
      // 结构和样式同时加上无过渡效果，需要加延时
      setTimeout(() => {
        fade.value = newVal
      }, 0)
    }, { immediate: true })

    // 自己关闭对话框 修改父组件的数据
    const closeDialog = () => {
      emit('update:visible', false)
    }
    return { fade, closeDialog }
  }
}
</script>

<style lang="less" scoped>
.xtx-dialog{
  // 遮罩层
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;
  background-color: rgba(0,0,0,0);
  &.fade{
    transition: all 0.4s;
    background-color:rgba(0,0,0,0.5);
  }
  .wrapper{
    width: 600px;
    background-color: #fff;
    border-radius: 4px;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-60%);
    opacity: 0;
    &.fade{
      transition: all 0.4s;
      transform: translate(-50%,-50%);
      opacity: 1;
    }
    .header{
      position: relative;
      left: 0;
      top: 0;
      line-height: 70px;
      padding: 0 20px;
      h3{
        font-weight: normal;
        font-size: 18px;
      }
      a{
        position: absolute;
        right: 25px;
        top:25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover{
          color: @xtxColor;
        }
      }
    }
    .body{
      padding: 20px 40px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
      font-weight: 16px;
    }
    .footer{
      padding: 20px;
      text-align: center;
    }
  }
}
</style>
